home *** CD-ROM | disk | FTP | other *** search
/ Just Call Me Internet / Just Call Me Internet.iso / archives / com / internet / sting / sting.lzh / STING / dialer / rem_ctrl / request.c < prev    next >
C/C++ Source or Header  |  1997-05-04  |  7KB  |  213 lines

  1.  
  2. /*********************************************************************/
  3. /*                                                                   */
  4. /*     This program requests remote control dialing from a Dialer    */
  5. /*      server. It serves as sample code for programmers.            */
  6. /*                                                                   */
  7. /*     (c) Peter Rottengatter              from 16. Januar 1997      */
  8. /*                                                                   */
  9. /*********************************************************************/
  10.  
  11.  
  12. #include <aes.h>
  13. #include <tos.h>
  14. #include <stdio.h>
  15. #include <stdlib.h>
  16. #include <time.h>
  17. #include <string.h>
  18.  
  19. #include "transprt.h"
  20. #include "remctrl.h"
  21.  
  22.  
  23. #define  DIAL_SCRIPT        "DIAL.SCR"
  24. #define  DIAL_MAX_NUMBER    5
  25. #define  TIMEOUT            60
  26.  
  27.  
  28. void  gem_program (void),  do_some_work (void);
  29. long  get_sting_cookie (void);
  30.  
  31.  
  32. typedef int  BOOLEAN;
  33.  
  34. DRV_LIST   *sting_drivers;
  35. TPL        *tpl;
  36. char       alert[200];
  37. int        gl_apid;
  38. int        errors[] = {  9, 
  39.                          IP_USER_ABORT, IP_IN_PROGRESS, IP_IS_CONNECTED,
  40.                          IP_SCR_NOT_FOUND, IP_MAX_EXCEEDED, IP_FATAL_PROBLEM,
  41.                          IP_OPEN_FAILED, IP_PORT_LOCKED
  42.                       };
  43.  
  44. char  not_there[] = "[1][ |  STinG is not loaded or enabled !   ][ Hmmm ]";
  45. char  corrupted[] = "[1][ |  STinG structures corrupted !   ][ Oooops ]";
  46. char  found_it[]  = "[3][ |  Driver \'%s\',|  by %s, found,   |  version %s.][ Okay ]";
  47. char  no_module[] = "[1][ |  STinG Transport Driver not found !   ][ Grmbl ]";
  48. char  no_dialer[] = "[1][ |  STinG variable DIALER not set !   ][ Grrr ]";
  49. char  not_start[] = "[1][ |  Dialer is not running !   ][ Forgot ]";
  50. char  begin_it[]  = "[3][ |  Dialer found. Sending dial request.   ][ Good ]";
  51. char  waiting[]   = "[3][ |  Waiting %d seconds for a response.   ][ Do it ]";
  52. char  dial_error_text[][70] = { "",
  53.                     "[1][ |  Dial error :| |    User aborted dialing !   ][ Uhmmm ]",
  54.                     "[1][ |  Dial error :| |    Dialing already in progress !   ][ Uhmmm ]",
  55.                     "[1][ |  Dial error :| |    Already connected !   ][ Uhmmm ]",
  56.                     "[1][ |  Dial error :| |    Script not found !   ][ Uhmmm ]",
  57.                     "[1][ |  Dial error :| |    Redials exceed max number !   ][ Uhmmm ]",
  58.                     "[1][ |  Dial error :| |    Modem reports fatal problem !   ][ Uhmmm ]",
  59.                     "[1][ |  Dial error :| |    Can't pass port to STinG !   ][ Uhmmm ]",
  60.                     "[1][ |  Dial error :| |    Can't open serial port !   ][ Uhmmm ]"
  61.                };
  62. char  success[]   = "[3][ |  Dialing done. Connection established.   ][ Well done ]";
  63. char  other_err[] = "[1][ |  Dialer reports an error.   | |    Code unknown !][ Hey ]";
  64. char  timer_qu[]  = "[2][ |  Wait timeout expired.| |    Give it some more time ?   ][ Yes | Abort ]";
  65. char  no_rslv[]   = "[1][ |  Attempt to resolve hostname   | |    failed !][ Piss off ]";
  66. char  bad_ip[]    = "[1][ |  Wrong format in IP address !  ][ F... ]";
  67. char  no_udp[]    = "[1][ |  Couldn't open UDP connection !  ][ Hmm ]";
  68.  
  69.  
  70.  
  71. void  main()
  72.  
  73. {
  74.    gl_apid = appl_init();
  75.  
  76.    gem_program();
  77.  
  78.    appl_exit();
  79.  }
  80.  
  81.  
  82. void  gem_program()
  83.  
  84. {
  85.    sting_drivers = (DRV_LIST *) Supexec (get_sting_cookie);
  86.  
  87.    if (sting_drivers == 0L) {
  88.         form_alert (1, not_there);
  89.         return;
  90.       }
  91.    if (strcmp (sting_drivers->magic, MAGIC) != 0) {
  92.         form_alert (1, corrupted);
  93.         return;
  94.       }
  95.  
  96.    tpl = (TPL *) (*sting_drivers->get_dftab) (TRANSPORT_DRIVER);
  97.  
  98.    if (tpl != (TPL *) NULL) {
  99.         sprintf (alert, found_it, tpl->module, tpl->author, tpl->version);
  100.         form_alert (1, alert);
  101.         do_some_work();
  102.       }
  103.      else
  104.         form_alert (1, no_module);
  105.  }
  106.  
  107.  
  108. long  get_sting_cookie()
  109.  
  110. {
  111.    long  *work;
  112.  
  113.    for (work = * (long **) 0x5a0L; *work != 0L; work += 2)
  114.         if (*work == 'STiK')
  115.              return (*++work);
  116.  
  117.    return (0L);
  118.  }
  119.  
  120.  
  121. void  do_some_work()
  122.  
  123. {
  124.    NDB      *ndb;
  125.    uint32   rhost;
  126.    time_t   timeout;
  127.    int      count, handle, message[12], abort;
  128.    char     *walk, dialer_name[64];
  129.  
  130.    strncpy (dialer_name, getvstr ("DIALER"), 64);
  131.  
  132.    if (strcmp (dialer_name, "0") == 0) {
  133.         form_alert (1, no_dialer);
  134.         return;
  135.       }
  136.  
  137.    if (atoi (dialer_name) == 0) {
  138.         if (resolve (dialer_name, NULL, &rhost, 1) <= 0) {
  139.              form_alert (1, no_rslv);
  140.              return;
  141.            }
  142.       }
  143.      else {
  144.         for (count = 0, walk = dialer_name; count < 4; count++) {
  145.              rhost = (rhost << 8) | atoi (walk);
  146.              if (count < 3) {
  147.                   if ((walk = strchr (walk, '.') + 1) == (void *) 1L) {
  148.                        form_alert (1, bad_ip);
  149.                        return;
  150.                      }
  151.                 }
  152.                else {
  153.                   if (strchr (walk, '.') != NULL) {
  154.                        form_alert (1, bad_ip);
  155.                        return;
  156.                      }
  157.                 }
  158.            }
  159.       }
  160.  
  161.    if ((handle = UDP_open (rhost, IP_DIALER_PORT)) < 0) {
  162.         form_alert (1, no_udp);
  163.         return;
  164.       }
  165.  
  166.    form_alert (1, begin_it);
  167.  
  168.    message[0] = IP_DIAL_REQUEST;
  169.    message[1] = DIAL_MAX_NUMBER;
  170.    strncpy ((char *) & message[2], DIAL_SCRIPT, 18);
  171.    message[11] = 0;
  172.  
  173.    UDP_send (handle, (char *) message, 24);
  174.  
  175.    sprintf (alert, waiting, (int) TIMEOUT);
  176.    form_alert (1, alert);
  177.  
  178.    timeout = time (NULL) + TIMEOUT;
  179.  
  180. /**/
  181. /*   Note that in case of IP_DIAL_ERROR : IP_OPEN_FAILED  a connection has been
  182. /*      established. If IP_DIAL_HANGUP is not used, your telephone bill will
  183. /*      start to grow ! ;-)
  184. /**/
  185.  
  186.    for (abort = 0; abort == 0;) {
  187.         evnt_timer (200, 0);
  188.         if ((ndb = CNget_NDB (handle)) != NULL) {
  189.              switch (* (int16 *) ndb->ndata) {
  190.                 case IP_DIAL_DONE :
  191.                   form_alert (1, success);
  192.                   abort = 1;
  193.                   break;
  194.                 case IP_DIAL_ERROR :
  195.                   for (count = 1; count < errors[0]; count++)
  196.                        if (errors[count] == * (int16 *) (ndb->ndata + 2))
  197.                             break;
  198.                   form_alert (1, (count == errors[0]) ? other_err : dial_error_text[count]);
  199.                   abort = 1;
  200.                   break;
  201.                 }
  202.            }
  203.         if (time (NULL) > timeout) {
  204.              if (form_alert (1, timer_qu) == 2)
  205.                   abort = 1;
  206.                else
  207.                   timeout = time (NULL) + TIMEOUT;
  208.            }
  209.       }
  210.  
  211.    UDP_close (handle);
  212.  }
  213.